import org.apache.commons.lang3.StringUtils;
import reactor.core.publisher.Mono;

/**
 * @auther zhouliangliang
 * @date 2022/9/29 16:10
 * @description
 */
public class AsyncWithReactor {
    private interface Callback{
       void doOnError();
       void doOnSuccess();
    }

    private static class A{
        public Mono invokeB(String value) {
            B b = new B();
            return Mono.create(sink -> {
                b.doSomething(value, new Callback() {
                    @Override
                    public void doOnError() {
                        System.out.println("in class A method doOnError!");
                        sink.error(new Exception("doOnError"));
                    }

                    @Override
                    public void doOnSuccess() {
                        System.out.println("in class A method doOnSuccess!");
                        sink.success("doOnSuccess");
                    }
                });
            });
        }
    }

    private static class B{
        public void doSomething(String value, Callback callback) {
            //do something
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if(StringUtils.isEmpty(value)) {
                callback.doOnError();
            } else {
                callback.doOnSuccess();
            }
        }
    }

    public static void main(String[] args) throws Exception{
        A a = new A();
        a.invokeB("aaaa").subscribe(result->{
            System.out.println(result);
        }, e->{
            System.out.println(e);
        });
    }
}
